<?php

/**
 * ajax actions.
 *
 * @package    DUYTAN
 * @subpackage ajax
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ajaxActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    return SfView::NONE;
  }

  public function executePermalink(sfWebRequest $request)
  {
	  $this->act = $request->getParameter('act');
	  
	  if ($this->act == 'show_popup')
	  {
		  $ground_id = 0;
		  if ($request->getParameter('ground_id'))
			  $ground_id = $request->getParameter('ground_id');
		  if ($ground_id)
		  {
			  $this->ground_id = $ground_id;
			  // GET ALL SCHEDULE FOR GROUND 
			  $q = Doctrine_Query::create()
				->from("DtSchedule s")
				->where("s.ground_id = ?", $ground_id);
			  $schedules = $q->fetchArray();
			  
			  
			  
			  
			  $this->schedules = $schedules;
			  
			  $scheduleChecked = array();
			  // GET 7 DATES FROM TODAY
			  $date = time();
			  $now = time();
			  $dates = array();
			  for ($i = 0; $i < 7; $i++)
			  {
				  $date = $now + $i*3600*24;
				  $dates[$i] = date("d/m/Y", $date);
				  
				  foreach ($schedules as $key => $schedule)
				  {
					  $order = Doctrine_Core::getTable('DtGroundOrder')
								->findOneByGroundIdAndScheduleAndReservedDate($ground_id, $schedule['schedule'], date('Y-m-d',$date));
					  $check = false;
					  
					  if ($i == 0)
					  {
						$end_time = substr($schedule['schedule'],0,5);
						$end_time = date("Y-m-d ").$end_time.":00";
						$end_time = strtotime($end_time);
						
						if ($end_time < time())
							$check = true;
					  }
					  
					  if (!$check && $order)
						if ($order->status != DtGroundOrder::STATUS_EXPIRE && $order->status != DtGroundOrder::STATUS_CANCEL)
						  $check = true;

					  $schedules[$key]['statusTemp'] = $check;
				  }
				  $scheduleChecked[$i] = $schedules;
			  }
			  
			  $this->scheduleChecked = $scheduleChecked;
			  $this->dates = $dates;
		  }
		  else {
		  }
	  } else if ($this->act == 'show_form')
	  {
		  $ground_id = 0;
		  if ($request->getParameter('ground_id'))
			  $ground_id = $request->getParameter('ground_id');
		  $this->ground_id = $ground_id;
		  $this->schedule = $request->getParameter('schedule');
		  $this->date = $request->getParameter('date');
		  
		  $q = Doctrine_Query::create()
			->from("DtFacility f");
		  $this->facilities = $q->fetchArray();
		  
	  } else if ($this->act == 'order')
	  {
		  $status = "ERROR !!! Xin vui lòng thử lại sau !";
		  $ground_id = $request->getParameter('ground_id');
		  $schedule = $request->getParameter('schedule');
		  $date = $request->getParameter('date');
		  $txt_telephone = $request->getParameter('telephone');
		  $txt_name = "";
		  if ($request->getParameter('name'))
			  $txt_name = $request->getParameter('name');
		  
		  $txt_email = "";
		  if ($request->getParameter('email'))
			  $txt_email = $request->getParameter('email');
		  
		  if ($ground_id) {
			  
			  $order_temp = Doctrine_Core::getTable('DtGroundOrder')
						->findOneByGroundIdAndScheduleAndReservedDate($ground_id, $schedule, $this->dmy2ymd($date));
			  $check = true;
			  
			  $schedule_obj = Doctrine_Core::getTable('DtSchedule')
			  					->findOneBySchedule($schedule);

			  if ($schedule_obj)
			  {
				$end_time = substr($schedule,0,5);
				$end_time = $this->dmy2ymd($date)." ".$end_time.":00";
				$end_time = strtotime($end_time);
				
				if ($end_time < time())
					$check = false;
			  }
			  
			  if ($check && $order_temp)
				if ($order_temp->status != DtGroundOrder::STATUS_EXPIRE && $order_temp->status != DtGroundOrder::STATUS_CANCEL)
				  $check = false;
				  
			  if ($check)
			  {
				  $customer = new DtCustomer();
				  $c = Doctrine_Core::getTable('DtCustomer')->findOneByPhoneNumber($txt_telephone);
				  if (!$c) {
					$customer->phone_number = $txt_telephone;
					$customer->name = $txt_name;
					$customer->email = $txt_email;
					$customer->created_at = date(DATE_ISO8601);
					$customer->updated_at = date(DATE_ISO8601);
				  	try {
						$customer->save();
				  	} catch (sfException $e) {
						//echo $e->getMessage();
				  	}
				  } else {
					$customer = $c;
				  }
				  
				  $order = new DtGroundOrder();
				  $order->customer_id = $customer->id;
				  $order->ground_id = $ground_id;
				  $order->schedule = $schedule;
				  $order->reserved_date = $this->dmy2ymd($date);
				  $order->created_at = date(DATE_ISO8601, time());
				  $order->updated_at = date(DATE_ISO8601, time());
				  $order->status = DtGroundOrder::STATUS_INIT;
				  $order->note = "";
				  $order->order_code = sha1($customer->id.$ground_id.$schedule.$order->reserved_date);
				  try {
					$order->customer_phone_number = $txt_telephone;
					$order->ground_name = Doctrine_Core::getTable('DtGround')->findOneById($ground_id)->name;
					$order->save();
					//$this->testSendMail();
					// report
					DtReportPerDay::createOrUpdateDailyReport($order);
				  } catch (sfException $e) {
					$check = false;
					//echo $e->getMessage();
				  }
				  
				  $q = Doctrine_Query::create()
					->from("DtFacility f");
				  $facilities = $q->fetchArray();
				  foreach ($facilities as $facility)
				  {
					  if ($request->getParameter('order_facility_'.$facility['id']) == 'on')
					  {
					  	  $order_facility = new DtGroundOrderFacilities();
						  $order_facility->ground_order_code = $order->order_code;
						  $order_facility->facility_id = $facility['id'];
						  $order_facility->save();
					  }
				  }
			  }
			  
			  if ($check)
				  $status = "Bạn đã đăng ký sân thành công !";
		  }
		  $this->check = $check;	
		  $this->status = $status;
	  }
	  return SfView::SUCCESS;
  }  
  
  public	function	dmy2ymd($time)
  {
	$kq = $time;
	if (strpos($time,":") > 0)
	{
		$y = substr($time,-6);
		$time = substr($time,0,-6);
	}

	if (strpos($time,"/")	> 0)
	{
		$x	=	explode("/",$time);
		$kq = $x[2]."/".$x[1]."/".$x[0];
	}
	else if (strpos($time,"-")	> 0)
	{
		$x	=	explode("-",$time);
		$kq = $x[2]."/".$x[1]."/".$x[0];
	}
	return $kq.(isset($y) ? $y : '');
  }
}
